package com.jiazhong.mingxing.project.bookstore.front.listener;

import com.alibaba.fastjson.JSONArray;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.jiazhong.mingxing.project.bookstore.commons.bean.Alipay;
import com.jiazhong.mingxing.project.bookstore.front.config.SpringMVCConfig;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.NumberFormat;

@Slf4j
// 退款
@Component

@RocketMQMessageListener(consumerGroup = "abc2",topic = "order",selectorExpression = "refund",messageModel = MessageModel.BROADCASTING)
public class RefundMoneyListener implements RocketMQListener<String> {

    @SneakyThrows
    public void onMessage(String s) {
        log.info("开始执行退款操作");
        // 1. 产生请求并设置id和退款金额
        NumberFormat format = new DecimalFormat("0.00");
        String formatNumber = format.format(s.split(",")[1]);
        Alipay alipay = new Alipay();
        alipay.setOut_trade_no(s.split(",")[0]);
        alipay.setRefund_amount(formatNumber);
        AlipayTradeRefundRequest refundRequest = new AlipayTradeRefundRequest();
        String jsonString = JSONArray.toJSONString(alipay);
        refundRequest.setBizContent(jsonString);
        // 2. 执行并获取结果
        AlipayTradeRefundResponse response = SpringMVCConfig.alipayClient.execute(refundRequest);
    }
}
